package com.ttc.mBackup.BackupImages;

import java.util.ArrayList;

import android.app.Activity;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.util.Log;


public class ImageManager {
	
	final public static int SDCARD = 0;
	final public static int PHONE = 1;
	final public static int ALL = 2;
	
	public static Bitmap getImage(String path){
		Bitmap bm = null;
		BitmapFactory.Options bfo = new BitmapFactory.Options();
		bfo.inSampleSize = 2;
		try{
			bm =  BitmapFactory.decodeFile(path, bfo);
		}catch (Exception e) {
			Log.v("mediastore", "Error at ImageManager.getImage: " + e.getMessage());
		}
		return bm;
	}
	
	public static Cursor getThumbailCursor_SDCard(Activity activity){
		String[] img = { MediaStore.Images.Thumbnails._ID };
		return  activity.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
				null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
	}
	
	
	public static Cursor getThumbailCursor_Phone(Activity activity){
		String[] img = { MediaStore.Images.Thumbnails._ID };
		return  activity.managedQuery(MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI, img, null,
				null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
	}
	
	public static Cursor getActualCursor_SDCard(Activity activity){
		String[] img = { MediaStore.Images.Media.DATA };
		return  activity.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, img, null, null, null);
	}
	
	//////// - testing - //////////////
	public static Cursor getThumbailCursorData_SDCard(Activity activity){
		String[] img = { MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Media.DATA  };
		return  activity.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img,
				null,
				null, null);
	}
	
	public static ArrayList<String> getThumbmailImagePath_SDCard(Activity activity) {
		ArrayList<String> listImages = new ArrayList<String>();

		Cursor actualimagecursor;
		int actual_image_column_index;


		actualimagecursor = getThumbailCursorData_SDCard(activity);
		while (actualimagecursor.moveToNext()) {
			actual_image_column_index = actualimagecursor
					.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
			String filename = actualimagecursor
					.getString(actual_image_column_index);

			listImages.add(filename);
			// Log.v("mediastore", "Adding bitmap: "+ filename);
		}

		return listImages;
	}
	//////// - testing - //////////////
	
	public static ArrayList<Bitmap> getThumbnailImages_SDCard(Activity activity) {
		ArrayList<Bitmap> images = null;
		BitmapFactory.Options bfo = new BitmapFactory.Options();
		bfo.inSampleSize = 2;

		ArrayList<String> imagePaths = getThumbmailImagePath_SDCard(activity);
		if (imagePaths != null && !imagePaths.isEmpty()) {
			images = new ArrayList<Bitmap>();
			for (String path : imagePaths) {
				Bitmap bm = BitmapFactory.decodeFile(path, bfo);
				images.add(bm);
			}
		}
		return images;
	}
	
	public static Cursor getActualCursor_Phone(Activity activity){
		String[] img = { MediaStore.Images.Media.DATA };
		return  activity.managedQuery(MediaStore.Images.Media.INTERNAL_CONTENT_URI, img, null, null, null);
	}
	
	public static ArrayList<String> getActualImagePath_All(Activity activity) {
		ArrayList<String> listImages = null;
		listImages = getActualImagePath_Phone(activity);
		
		ArrayList<String> imgOnSdcard = getActualImagePath_SDCard(activity);
		
		if(listImages == null) listImages = new ArrayList<String>();
		if(imgOnSdcard!=null) listImages.addAll(imgOnSdcard);
		
		return listImages;
	}

	public static ArrayList<String> getActualImagePath_SDCard(Activity activity) {
		ArrayList<String> listImages = new ArrayList<String>();

		Cursor actualimagecursor;
		int actual_image_column_index;


		actualimagecursor = getActualCursor_SDCard(activity);
		while (actualimagecursor.moveToNext()) {
			actual_image_column_index = actualimagecursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			String filename = actualimagecursor
					.getString(actual_image_column_index);

			listImages.add(filename);
			// Log.v("mediastore", "Adding bitmap: "+ filename);
		}

		return listImages;
	}

	public static ArrayList<String> getActualImagePath_Phone(Activity activity) {
		ArrayList<String> listImages = new ArrayList<String>();

		Cursor actualimagecursor;
		int actual_image_column_index;


		actualimagecursor = getActualCursor_Phone(activity);
		while (actualimagecursor.moveToNext()) {
			actual_image_column_index = actualimagecursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			String filename = actualimagecursor
					.getString(actual_image_column_index);

			listImages.add(filename);
			// Log.v("mediastore", "Adding bitmap: "+ filename);
		}

		return listImages;
	}
	
	public static ArrayList<Bitmap> getActualImages_SDCard(Activity activity) {
		ArrayList<Bitmap> images = null;
		BitmapFactory.Options bfo = new BitmapFactory.Options();
		bfo.inSampleSize = 2;

		ArrayList<String> imagePaths = getActualImagePath_SDCard(activity);
		if (imagePaths != null && !imagePaths.isEmpty()) {
			images = new ArrayList<Bitmap>();
			for (String path : imagePaths) {
				Bitmap bm = BitmapFactory.decodeFile(path, bfo);
				images.add(bm);
			}
		}
		return images;
	}
	
	public static ArrayList<Bitmap> getActualImages_Phone(Activity activity) {
		ArrayList<Bitmap> images = null;
		BitmapFactory.Options bfo = new BitmapFactory.Options();
		bfo.inSampleSize = 2;

		ArrayList<String> imagePaths = getActualImagePath_Phone(activity);
		if (imagePaths != null && !imagePaths.isEmpty()) {
			images = new ArrayList<Bitmap>();
			for (String path : imagePaths) {
				Bitmap bm = BitmapFactory.decodeFile(path, bfo);
				images.add(bm);
			}
		}
		return images;
	}
	
	public static ArrayList<Bitmap> getActualImages_All(Activity activity) {
		ArrayList<Bitmap> images = null;
		BitmapFactory.Options bfo = new BitmapFactory.Options();
		bfo.inSampleSize = 2;

		ArrayList<String> imagePaths = getActualImagePath_All(activity);
		if (imagePaths != null && !imagePaths.isEmpty()) {
			images = new ArrayList<Bitmap>();
			for (String path : imagePaths) {
				Bitmap bm = BitmapFactory.decodeFile(path, bfo);
				images.add(bm);
			}
		}
		return images;
	}

	public static ArrayList<Bitmap> getActualImages(Activity activity, int type){
		switch (type) {
		case SDCARD:
			return getActualImages_SDCard(activity);
		case PHONE:
			return getActualImages_Phone(activity);
		case ALL:
			return getActualImages_All(activity);
		default:
			return null;
		}
	}
	
	public static ArrayList<String> getActualImagePath(Activity activity, int type){
		switch (type) {
		case SDCARD:
			return getActualImagePath_SDCard(activity);
		case PHONE:
			return getActualImagePath_Phone(activity);
		case ALL:
			return getActualImagePath_All(activity);
		default:
			return null;
		}
	}
}
